package com.fare.taluo.core.domain.config.line.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.dayatang.hibernate.EntityRepositoryHibernate;
import com.fare.taluo.core.domain.config.line.UserCareLineEntity;
import com.fare.taluo.core.domain.config.line.UserCareLineEntityRepository;
import com.fare.taluo.util.StringUtils;

@Repository
public class UserCareLineEntityRepositoryImpl extends EntityRepositoryHibernate
		implements UserCareLineEntityRepository {
	
	@Override
	public List<UserCareLineEntity> findLine(Long userId) {
		List<Object> list = new ArrayList<Object>();
		StringBuffer sbf = new StringBuffer(
				"from UserCareLineEntity u where u.userId=? ");
		sbf.append("  order by u.priority, u.createTime desc");
		list.add(userId);
		return super.find(sbf.toString(),list.toArray(), UserCareLineEntity.class);	
	}

	@Override
	public List<UserCareLineEntity> findLine(Long userId, String departcity,
			String arrivalcity) {
		List<Object> list = new ArrayList<Object>();
		StringBuffer sbf = new StringBuffer(
				"from UserCareLineEntity p where p.userId=? ");
		list.add(userId);
		if (StringUtils.isNotEmpty(departcity)) {
			sbf.append("and p.departcitycode =? ");
			list.add(departcity);
		}
		if (StringUtils.isNotEmpty(arrivalcity)) {
			sbf.append("and p.arrivalcitycode =? ");
			list.add(arrivalcity);
		}
		sbf.append("  order by p.priority, p.createTime desc");
		
		return super.find(sbf.toString(),
				list.toArray(), UserCareLineEntity.class);	
	}

}
